#!/bin/bash
# 
# curl https://api-beta.yjzf.com/open.core.srv/v2/api-docs -o open.core.srv_1.3.23-RELEASE.json
# git add .
# git commit -m 'open.core.srv_1.3.23-RELEASE.json'
# git push



if [ ! -n "$1" ]; then
  echo " e.g. | ./pkg.sh open.core.srv_2.11.0-RELEASE"
  echo "app version must not be null. exit 1"
  exit 1 
fi

input=$1

array=(${input//_/ })

plen=${#array[@]}

if [ $plen != 2 ]; then
  echo "pls input {app_key}_{version} . exit 1"
  exit 1 
fi


app_key=${array[0]}
version=${array[1]}

echo 'app_key:' $app_key
echo 'version:' $version


worksp='D:\code\api-spec\'$app_key

if [ ! -d "$worksp" ]; then
  echo $worksp "not exists. exit 1"
  exit 1 
fi

declare -A myMap=(
["open.core.srv"]="8447" 
["common.core.srv"]="8070" 
["mall.core.srv"]="8551" 
["inf.uac.srv"]="8055" 
["yjyz.web.srv"]="8002" 
["inf.search.srv"]="8015" 
["yjyz.search.srv"]="8015" 
["erp.customer.srv"]="20001"
["lee"]="c"
)

port=${myMap[$app_key]}
domain='http://localhost:'$port

if [ ! -n "$port" ]; then
  echo "app_key port not exists. default beta env"
  domain='https://api-beta.yjzf.com'
fi



cd D:/code/api-spec/

git fetch --all
git reset --hard
git pull -f origin

cd $worksp


# url=https://api-beta.yjzf.com/$app_key/v2/api-docs
# url=http://localhost:8447/$app_key/v2/api-docs
# url=http://localhost:8070/$app_key/v2/api-docs

url=$domain/$app_key'/v2/api-docs'
#echo $url


filename=$app_key'_'$version.json
echo 'filename:' $filename

echo 'curl' $url '>>' $filename

curl -s $url -o $filename

if [ ! -f "$filename" ]; then
  echo "api-spec.json not exists. exit 1"
  exit 1 
fi

#git pull --all
git add $app_key'*'
git commit -m "push $app_key $filename" 
git push
git status

echo "push $app_key $filename success!"